package com.chevic.app;

import java.sql.Connection;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import com.chevic.util.DBUtil;
import com.chevic.util.Util;

public class Count {
	
	public String saveCount(HttpServletRequest req) throws Exception{
		String type = req.getParameter("type") ;
		String sql = "" ;
		String[] args={} ;
		
		String userName = req.getParameter("userName") ;
		String ip = req.getParameter("ip") ;
		String logtime = Util.getCurrentTime() ;
		String point = "" +this.getPoint();
		
		if("phone".equals(type)){
			sql = "insert into testlog (userName,telNum,ip,type,logtime,point) values(?,?,?,?,?,?)" ;
			String telNum = req.getParameter("telNum") ;
			args = new String[]{userName,telNum,ip,type,logtime,point} ;
		}else if("qq".equals(type)){
			sql = "insert into testlog (userName,qqNum,ip,type,logtime,point) values(?,?,?,?,?,?)" ;
			String qqNum = req.getParameter("qqNum") ;
			args = new String[]{userName,qqNum,ip,type,logtime,point} ;
		}else if("ta".equals(type)){
			sql = "insert into testlog (userName,taName,userAge,taAge,ip,type,logtime,point) values(?,?,?,?,?,?,?,?)" ;
			String taName = req.getParameter("taName") ;
			String userAge = req.getParameter("userAge") ;
			String taAge = req.getParameter("taAge") ;
			args = new String[]{userName,taName,userAge,taAge,ip,type,logtime,point} ;
		}
		
		if(!"".equals(sql)){
			Connection conn = DBUtil.getConnR() ;
			DBUtil.executeSql(conn, sql, args) ;
			conn.close() ;
		}
		return point ;
	}
	
	private int getPoint(){
		Random r = new Random() ;
		int point1 = r.nextInt(40) ;
		int flag = r.nextInt(2) ;
		int point=0 ;
		if(flag==0){
			point = 60+point1 ;
		}else{
			point = 60-point1 ;
		}
		return point ;
	}
	

}
